/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.hibernate;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.piaoyi.bean.Price;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.PriceDAO;

/**
 * 功能描述:价格操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class PriceHibernateDAO extends HibernateDaoSupport implements PriceDAO {

	private static final String LOAD_BY_ID = "from Price where id = ?";
	private static final String  REMOVE_BY_ID = "delete from Price where id = ?";
	
	/**
	 * 保存Price对象
	 * 
	 * @param price
	 * 			Price对象
	 * @return Price
	 * 			Price对象
	 */
	public Price savePrice(Price price){
		this.getHibernateTemplate().save(price);
		return price;
	}
	/**
	 * 保存或更新Price对象
	 * 
	 * @param price
	 * 			Price对象
	 * @return Price
	 * 			Price对象
	 */
	public Price saveOrUpdatePrice(Price price){
		this.getHibernateTemplate().saveOrUpdate(price);
		return price;
	}
	/**
	 * 保存Price对象
	 * 
	 * @param price
	 * 			Price对象
	 * @return Price
	 * 			Price对象
	 */
	public Price updatePrice(Price price){
		this.getHibernateTemplate().update(price);
		return price;
	}
	
	/**
	 * 根据主键ID获取Price对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Price
	 * 			Price对象
	 */
	public Price findPriceById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Price) l.get(0);
		}
	}
	
	/**
	 * 获取Price个数
	 * 
	 * @param dailyId
	 * 			long
	 * @return int
	 * 			Price个数
	 */
	public long getDailyNum(final long dailyId){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Price.class);
				c.setProjection(Projections.count("id"));
				if (dailyId != -1) {
					c.add(Restrictions.eq("dailyId", new Long(dailyId)));
				}
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Integer) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取Price列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param dailyId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Price对象列表
	 */
	public List findPrices(final long goodsId,final long dailyId,final String orderby, final int ascOrDesc, final int firstResult, final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Price.class);
				if (goodsId != -1) {
					c.add(Restrictions.eq("goodsId", new Long(goodsId)));
				}
				if (dailyId != -1) {
					c.add(Restrictions.eq("dailyId", new Long(dailyId)));
				}
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
	
	/**
	 * 获取Price列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param dailyId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Daily对象列表
	 */
	public List findPrices(final long goodsId,final long dailyId,final String orderby, final int ascOrDesc){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Price.class);
				if (goodsId != -1) {
					c.add(Restrictions.eq("goodsId", new Long(goodsId)));
				}
				if (dailyId != -1) {
					c.add(Restrictions.eq("dailyId", new Long(dailyId)));
				}
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				return c.list();
			}
		});
	}
	
	/**
	 * 获取PriceID列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param dailyId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			PriceID对象列表
	 */
	public List findPricePrices(final long goodsId,final long dailyId,final String orderby, final int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("select price from Price where 1=1");
		if (goodsId != -1) {
			sb.append(" and goodsId=");
			sb.append(goodsId);
		}
		if (dailyId != -1) {
			sb.append(" and dailyId=");
			sb.append(dailyId);
		}
		if (StringUtils.isNotBlank(orderby)) {
			sb.append(" order by ");
			sb.append(orderby);
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" desc");
			}
		}
		return this.getHibernateTemplate().find(sb.toString());
	}
	
	public List findPriceGoodsIds(long startPrice,long endPrice){
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct goodsId from Price where 1=1");
		if (startPrice != -1) {
			sb.append(" and price>=");
			sb.append(startPrice);
		}
		if (endPrice != -1) {
			sb.append(" and price<=");
			sb.append(endPrice);
		}
		return this.getHibernateTemplate().find(sb.toString());
	}
	
	/**
	 * 删除Price对象
	 */
	public void removePriceById(final long id){
	    getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
}
